/* bmpsxr.p - BOM MASTER RESPONSIBILITY-OWNING PROGRAM                        */
/* Copyright 1986-2000 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.                        */
/* $Revision: 1.5 $                                                           */
/*                                                                            */
/* This routine contains procedures pertaining to Bill Of Material            */
/*                                                                            */
/* Revision: 1.1      BY: Satish Chavan       DATE: 03/23/00   ECO: *N03T*   */
/* Revision: 1.2      BY: Satish Chavan       DATE: 05/16/00   ECO: *N0B9*   */
/* Revision: 1.3      BY: Paul Donnelly       DATE: 07/20/00   ECO: *N0DP*   */
/* $Revision: 1.5 $     BY: Evan Bishop      DATE: 08/10/00     ECO: *N0JP*    */
/* REVISION: 9.1    LAST MODIFIED: 08/11/00 BY: *N0KK* jyn                             */
/*                                                                            */
/*V8:ConvertMode=NoConvert                                                    */

{mfdeclre.i}
{pxmaint.i}

/* ========================================================================= */
/* ******************************* FUNCTIONS ******************************* */
/* ========================================================================= */


/* ========================================================================= */
/* ************************* INTERNAL PROCEDURES *************************** */
/* ========================================================================= */

/*===========================================================================*/
PROCEDURE processCreate:
/*---------------------------------------------------------------------------
   Purpose:     Creates a record in the Bill of Materials master buffer.
   Exceptions:  NONE
   Notes:
   History:
---------------------------------------------------------------------------*/
   define input parameter pBomMasterItem as character no-undo.
   define parameter buffer pBomMasterBuffer for bom_mstr.

   define variable addPSValue as character initial "ADD-PS".
   define buffer pt_mstr_buffer for pt_mstr.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

      /* Validate that the Parent Id is valid */
      {pxrun.i &PROC        = 'validateParentId'
               &PARAM       = "(input pBomMasterItem)"
      }
     /* guhs
      {pxrun.i &PROC = 'validateItemStatus'
              &PARAM = "(input pBomMasterItem, input addPSValue)"
      }
     */
      create pBomMasterBuffer.
      assign bom_parent = pBomMasterItem.

      for first pt_mstr_buffer fields (pt_formula pt_part pt_um)
      no-lock where pt_part = pBomMasterItem :

            assign bom_parent = pt_part
                 bom_batch_um = pt_um
                 bom_formula  = pt_formula.
      end.

      if recid(pBomMasterBuffer) = -1 then .

   end. /* do on error undo, return {&GENERAL-APP-EXCEPT}: */
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* createBomMaster */

/*===========================================================================*/
PROCEDURE processDelete:
/*---------------------------------------------------------------------------
   Purpose:     Deletes BOM Master record buffer.
   Exceptions:  NONE
   Notes:
   History:
---------------------------------------------------------------------------*/
   define parameter buffer pBomMasterBuffer for bom_mstr.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

 /* NOTE: ADD LOGIC TO CHECK FOR EXISTING PRODUCT STRUCTURE RECORDS PRIOR TO
          DELETE OF BOM MASTER RECORD */

 /* NOTE: NEED TO DELETE COMMENTS ATTACHED TO BOM MASTER RECORD */

      /* DELETE BOM MASTER RECORD */
      delete pBomMasterBuffer.

   end. /* do on error undo, return {&GENERAL-APP-EXCEPT}: */
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* deleteBomMaster */

/*===========================================================================*/
PROCEDURE processRead:
/*---------------------------------------------------------------------------
   Purpose:     Reads BOM Master record depending on the lock & wait flags
   Exceptions:  NONE
   Notes:
   History:
---------------------------------------------------------------------------*/
   define input parameter pBomMasterCode as character no-undo.

   define parameter buffer pBomMasterBuffer for bom_mstr.
   define input parameter pLockFlag as logical no-undo.
   define input parameter pWaitFlag as logical no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

      if plockflag then do:
         if PWaitFlag then do:
            find pBomMasterBuffer
            where bom_parent = pBomMasterCode
            exclusive-lock no-error.
         end.
         else do:
            find pBomMasterBuffer
            where bom_parent = pBomMasterCode
            exclusive-lock no-wait no-error.

            if locked pBomMasterBuffer
               then return {&RECORD-LOCKED}.
         end.

         if not available pBomMasterBuffer
            then return {&RECORD-NOT-FOUND}.

      end. /* if plockflag then do: */
      else do:

         for first pBomMasterBuffer no-lock
         where bom_parent = pBomMasterCode:
            return {&SUCCESS-RESULT}.
         end.

         return {&RECORD-NOT-FOUND}.
      end.
   end. /* do on error undo, return error {&GENERAL-APP-EXCEPT}: */

   return {&SUCCESS-RESULT}.

END PROCEDURE. /* processRead */

/*===========================================================================*/
PROCEDURE setModificationInfo:
/*---------------------------------------------------------------------------
   Purpose:     Stores audit entries into product structure buffer
   Exceptions:  NONE
   Notes:
   History:
---------------------------------------------------------------------------*/
   define parameter buffer pProductStructureMaster for bom_mstr.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

      /* STORE MODIFY DATE AND USERID */
      assign
         bom_mod_date = today
         bom_userid   = global_userid.
   end. /* do on error undo, return {&GENERAL-APP-EXCEPT}: */
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* setModificationInfo */

/*===========================================================================*/
PROCEDURE validateBlankBomFsmType:
/*---------------------------------------------------------------------------
  Purpose:     Validate that BOM FSM type is blank
  Exceptions:  NONE
  Notes:
  History:
---------------------------------------------------------------------------*/
   define input parameter pBomType as character no-undo.

   define variable blankStatusValue as character no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

     if pBomType <> blankStatusValue
     then do:
        if pBomType = "FSM"
        then do:
           /* 7487 - This is a service bill of material, not a standard bom */
           {pxmsg.i &MSGNUM=7487 &ERRORLEVEL={&APP-ERROR-RESULT}}
           return error {&APP-ERROR-RESULT}.
        end.
     end.
  end.
  return {&SUCCESS-RESULT}.
END PROCEDURE. /* validateBlankBomFsmType */

/*===========================================================================*/
PROCEDURE validateBomCodeisItem:
/*---------------------------------------------------------------------------
     Purpose:  Validate that the bom code exists as an item
  Exceptions:  NONE
       Notes:
     History:
---------------------------------------------------------------------------*/
   define input parameter pBomCode as character no-undo.

   define buffer itemMasterBuffer for pt_mstr.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

     /* Lookup item record */
     if not can-find (itemMasterBuffer where pt_part = pBomCode)
     then do:
        /* 231  - Item number or bom code must exist */
        {pxmsg.i &MSGNUM=231 &ERRORLEVEL={&APP-ERROR-RESULT}}
        return error {&APP-ERROR-RESULT}.
     end.
  end.
  return {&SUCCESS-RESULT}.
END PROCEDURE. /* validateBomCodeisItem */

/*===========================================================================*/
PROCEDURE validateBomCodeNotFormula:
/*---------------------------------------------------------------------------
  Purpose:     Validate that BOM code is not formula controlled
  Exceptions:  NONE
  Notes:
  History:
---------------------------------------------------------------------------*/
   define input parameter pBomFormula as logical no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

     if pBomFormula = yes
     then do:
        /* 263 - Formula controlled */
        {pxmsg.i &MSGNUM=263 &ERRORLEVEL={&APP-ERROR-RESULT}}
        return error {&APP-ERROR-RESULT}.
     end.
  end.
  return {&SUCCESS-RESULT}.
END PROCEDURE. /* validateBomCodeNotFormula */

/*===========================================================================*/
PROCEDURE validateItemStatus:
/*---------------------------------------------------------------------------
  Purpose:     Validate that item has status that permits transaction
  Exceptions:  NONE
  Notes:
  History:
---------------------------------------------------------------------------*/

   define input parameter pItem as character no-undo.
   define input parameter pTransactionCode as character no-undo.

   define variable statusValue as character no-undo.
   define buffer itemMasterBuffer for pt_mstr.
   define buffer itemStatusBuffer for isd_det.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

     /* Lookup item status value for item */
     for first itemMasterBuffer
     fields (pt_part pt_status)
     no-lock where pt_part = pItem:
        statusValue = pt_status.
        substring (statusValue,9,1) = "#".

        /* See if requested transaction is restricted for item status */
        if can-find (itemStatusBuffer where isd_status = statusValue
        and isd_tr_type = pTransactionCode)
        then do:
           /* 358  - Restricted procedure for item status code */
           {pxmsg.i &MSGNUM=358 &ERRORLEVEL={&APP-ERROR-RESULT}}
           return error {&APP-ERROR-RESULT}.
        end.
     end.
  end.
  return {&SUCCESS-RESULT}.
END PROCEDURE. /* validateItemStatus */

/*============================================================================*/
PROCEDURE validateParentId:
/*---------------------------------------------------------------------------
   Purpose:     TO VALIDATE THE ENTERED PARENT ID FOR BLANK VALUE
   Exceptions:  NONE
   Notes:
   History:
---------------------------------------------------------------------------*/
   define input parameter pParentId as character no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

      if pParentId = "" then do:

         /* 40 - BLANK NOT ALLOWED. */
         {pxmsg.i &MSGNUM     = 40
                  &ERRORLEVEL = {&APP-ERROR-RESULT}
                   &FIELDNAME = ""bom_parent""
         }
         return error {&APP-ERROR-RESULT}.

      end. /* if input pParentId = "" then do: */

   end. /* do on error undo, return error {&GENERAL-APP-EXCEPT}: */
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* validateParentId */

/*============================================================================*/
PROCEDURE validateNoCyclicStructure:
/*---------------------------------------------------------------------------
   Purpose:     TO GENERATE ERROR MESSAGE IF USER ENTERS BOM CODE AS THE
                ITEM CODE
   Exceptions:  APP-ERROR-RESULT
   Notes:       Used by item maintenance (N03T). Should be moved to bmpscxr.p
   History:
  ---------------------------------------------------------------------------*/
   define input parameter pPsRecno as integer no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if pPsRecno = 0 then do:
         /* 206 - CYCLIC PRODUCT STRUCTURE NOT ALLOWED */
         {pxmsg.i &MSGNUM     = 206
                  &ERRORLEVEL = {&APP-ERROR-RESULT}}
         return error {&APP-ERROR-RESULT}.
      end. /* if pPsRecno = 0 */
   end. /* do on error undo, return error {&GENERAL-APP-EXCEPT}: */
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* validateNoCyclicStructure */

/*============================================================================*/
PROCEDURE validateParentItem:
/*-----------------------------------------------------------------------------
   Purpose:     TO CHECK IF BOM MASTER EXISTS FOR THE USER-ENTERED BOM CODE
   Exceptions:  NONE
   Notes:       Used by item maintenance (N03T). Should be moved to bmpscxr.p
   History:
------------------------------------------------------------------------------*/
   define input parameter pParent    as character no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

      if not can-find(first ps_mstr
         where ps_par = pParent) then do:
         /* 100 - NO BILL OF MATERIAL EXISTS */
         {pxmsg.i &MSGNUM     = 100
                  &ERRORLEVEL = {&WARNING-RESULT}
                  &PAUSEAFTER = TRUE}
         return {&WARNING-RESULT}.
      end. /* if not can-find(first ps_mstr */

   end. /* do on error undo, return error {&GENERAL-APP-EXCEPT}: */
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* validateParentItem */
